cc.Class({
    extends: cc.Component,

    properties: {
        ballAudio: {
            type: cc.AudioClip,
            default: null
        },
        hitAudio: {
            type: cc.AudioClip,
            default: null
        }
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        // 球方向随机
        this.node.getComponent(cc.RigidBody).linearVelocity.x = Math.random() * -1200 + 600;
        this.node.getComponent(cc.RigidBody).linearVelocity.y = 1000;
    },

    init(game) {
        this.game = game;
    },

    onBeginContact (contact, self, other) {
        if (other.tag == 1) {
            cc.audioEngine.playEffect(this.ballAudio);
        }
        else if (other.tag == 2) {
            other.node.opacity = 0;                             // 变透明
            other.node.removeComponent(cc.RigidBody);           // 移出刚体组件，让该砖块无法与球碰撞
            cc.audioEngine.playEffect(this.hitAudio);

            // 根据透明度循环判断砖块是否都被消灭
            let isWin = true;
            for (let i=0; i<this.game.bricksArray.length; i++) {
                if (this.game.bricksArray[i].opacity!=0) {
                    isWin = false;
                }
            }
            if (isWin) {
                this.game.win();
            }
        }
    }

    // update (dt) {},
});
